package com.java.asvitelskyy.company;

import java.util.Random;

public class Company {

    public int count = 0;
    public Worker[] workers;

    public Company() {
        this.workers = new Worker[count];
    }

    private void grow() {
        if (count == workers.length) {
            Worker[] tmp = new Worker[1 + count * 2];
            for (int index = 0; index < count; index++) {
                tmp[index] = this.workers[index];
            }
            this.workers = tmp;
        }
    }

    public void sortByName() {
        int start;
        for (int index = 1; index < count; index++) {
            Worker tmp = workers[index];
            start = index;
            while (start > 0 && workers[start - 1].getName().compareTo(tmp.getName()) > 0) {
                workers[start] = workers[start - 1];
                --start;
            }
            workers[start] = tmp;
        }
    }

    public void sortBySalary() {
        int start;
        Main.print(String.valueOf(workers.length));
        for (int index = 1; index < count; index++) {
            Worker tmp = workers[index];
            start = index;
            while (start > 0 && workers[start - 1].calcSalary() < tmp.calcSalary()) {
                workers[start] = workers[start - 1];
                --start;
            }
            workers[start] = tmp;
        }
    }

    public void listWorkers() {
        for (int index = 0; index < count; index++) {
            Main.print("|" + String.format("%10s ", index + 1) + "| " + workers[index].getWorker());
        }
    }

    public void sortById() {
        for (int index = 0; index < count; index++) {
            if (workers[index].getId() > workers[index + 1].getId()) {

            }
        }

    }

    public void addFixWorker() {
        Main.print("Enter name of new worker");
        String name = Main.getInputString();
        Main.print("Enter worker salary");
        int salary = Main.getInputInt();
        grow();
        this.workers[count] = new FixWorker(name, rand(), salary);
        count++;
    }

    public void addHourWorker() {
        Main.print("Enter name of new worker");
        String name = Main.getInputString();
        Main.print("Enter worker time of work");
        int time = Main.getInputInt();
        grow();
        this.workers[count] = new HourWorker(name, rand(), time);
        count++;
    }

    public void deleteWorker(int id) {
        for (int index = 0; index < count; index++) {
            if (index > id - 1) {
                this.workers[index - 1] = this.workers[index];
            }
        }
        this.workers[--count] = null;
    }

    public int rand() {
        Random result = new Random();
        return result.nextInt(999);
    }
}
